<?php
namespace app\wap\controller;

use think\Controller;
use app\wap\model\Member;
use app\wap\model\Project as Projects;
use app\index\model\Addonarticle;
use app\index\model\Archive;
use app\index\model\Order;
use app\index\model\Tags;
use think\Db;
use think\Request;
use think\Config;

class Project extends Common
{

    const PERPAGE = 10;
    public function _initialize(){
        parent::_initialize();

    }

    /**
     * 认投项目列表
     * @param Request $request
     * @author hey_heyang(635870453@qq.com)
     * @return array()
     */
    public function projectList(Request $request){
        if($request->isPost()) {
            $user_info = $request->user_info;
            $param = $request->param();
            if (empty($param['curpage'])) {
                $curpage = 1;
            } else {
                $curpage = $param['curpage'];
            }
            $pmodel = new Projects;
            $status = $param['status'];
            if($status>0){
                $where['p.status'] = $status == 3 ? ['>=',3] : $status;
            }else{
                $where['p.status'] = ['>',0];
            }
            $where['p.id']=['not in',[30,35]];
//            $where['p.id'] = ['<',53];
            $dataList = $pmodel->getProjecList($where);
            $total_count = count($dataList);
            $total_page = ceil($total_count/self::PERPAGE);
            $data_list = pageArray($dataList,self::PERPAGE,$curpage);
//            if(count($data_list)){
//                foreach($data_list as $key=>$value){
//                    $data_list[$key]['remaining_time'] = OverTime($value['end_time']);
//                }
//            }
            $res_data =array('project_list'=>$data_list,'total_page'=>$total_page,'total_count'=>$total_count) ;
            outPutData($res_data);

        }
    }

    /**
     * 认投项目详情
     * @param Request $request
     * @author hey_heyang(635870453@qq.com)
     * @return array
     */
    public function projectDetail(Request $request){
        if($request->isPost()) {
            $user_info = $request->user_info;
            $uid =$user_info['id'];
            $param = $request->param();
            $pmodel = new Projects;
            $orModel = new Order();
            $tagModel = new Tags();
            $project_id = $param['project_id'];
            if($project_id==30 || $project_id==35){
                outPutData('','查看项目失败',Config::get('code.tipsCode'));
            }
            if(empty($project_id) || $project_id<0){
                outPutData('','无效参数',Config::get('code.tipsCode'));
            }else{
                $where['p.id'] = $project_id;
            }
            $my_order_money = $orModel->where(array('uid'=>$uid,'project_id'=>$project_id,'status'=>['gt',1],'type'=>1))->sum('money');
            $project_info = $pmodel->getProjecDetail($where);
            if(!empty($project_info['thumb'])){
                $img = explode(',', $project_info['thumb']);
                $project_info['thumb'] = $img;
            }
            if(empty($project_info)){
                outPutData('','未查询到相关信息',Config::get('code.tipsCode'));
            }
            $project_info['remaining_time'] = OverTime($project_info['end_time']);  //剩余天数
            $project_info['tags'] = unserialize($project_info['tags']); // 标签
            $tags=array();
            if(count($project_info['tags'])){
                foreach($project_info['tags'] as $k=>$v){
                   $tags[$k]=$tagModel->TagName($v);
                }
            }
            $project_info['tags']=$tags;
            $project_info['percent'] = GetProjectBar($project_id,1); // 进度百分比
            $project_info['has_order_money'] = GetProjectBar($project_id); //已筹金额
            $project_info['my_order_money'] = $my_order_money;

            //项目动态
            $arcModel = new Archive();
            $newList = $arcModel->where(['status'=>1,'project_id'=>$project_id])->select();
            $newList = empty($newList)?array():$newList->toArray();
            $project_info['new_list'] =$newList;

            $res_data= $project_info;
            outPutData($res_data);

        }
    }



}
